<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    * {
      font-size: 28px;
    }
  </style>
  <body>
    <div>测试fetch 界面</div>
    <button id="btnSend">发送请求</button>
    <button id="btnCancel">取消请求</button>
    <script>
      const controller = new AbortController();
      const signal = controller.signal;

      btnSend.onclick = function sendFetch(test) {
        fetch("http://127.0.0.1:3000/fetch", { signal })
          .then((response) => {
            return response.text();
          })
          .then((text) => {
            console.log(text);
          });
      };

      btnCancel.onclick = function () {
        console.log("取消请求");
        controller.abort();
      };
    </script>
  </body>
</html>
